package com.mcgrady.start;

import org.junit.Test;

/**
 * @author McGrady
 */
public class StringTest {
    @Test
    public void build() {
        String strA = new String("hello strA!!!!");
        System.out.println(strA);

        String strB = "hello strB!!!!";
        System.out.println(strB);
    }

    /**
     * char charAt​(int index)
     * Returns the char value at the specified index.
     * 返回指定索引处的char值。
     */
    @Test
    public void charAtTest() {
        String str = "hello world...";
        char result = str.charAt(6);
        System.out.println(result);
    }

    /**
     * String concat​(String str)
     * Concatenates the specified string to the end of this string.
     * 将指定的字符串连接到该字符串的末尾。
     */
    @Test
    public void concatTest() {
        String str = "Hello";
        String result = str.concat(" World!");
        System.out.println(result);
    }

    /**
     * boolean endsWith​(String suffix)
     * Tests if this string ends with the specified suffix.
     * 测试此字符串是否以指定的后缀结尾。
     */
    @Test
    public void endsWithTest() {
        String str = "Hello World!";
        boolean result = str.endsWith("d!");
        System.out.println(result);
    }

    /**
     * boolean equalsIgnoreCase​(String anotherString)
     * Compares this String to another String, ignoring case considerations.
     * 不考虑大小写考虑，将此字符串与另一个字符串进行比较。
     */
    @Test
    public void equalsIgnoreCaseTest() {
        String str = "Hello World!";
        boolean result = str.equalsIgnoreCase("hello world!");
        System.out.println(result);
        ;
    }

    /**
     * int indexOf​(int ch)
     * Returns the index within this string of the first occurrence of the specified character.
     * 返回指定字符首次出现在此字符串中的索引。
     */
    @Test
    public void indexOfTest() {
        String str = "Hello World!";
        int result = str.indexOf('o');
        System.out.println(result);
    }

    /**
     * boolean isEmpty()
     * Returns true if, and only if, length() is 0.
     * 当且仅当length（）为0时，才返回true。
     */
    @Test
    public void isEmptyTest() {
        String str1 = "Hello";
        String str2 = "";
        boolean result1 = str1.isEmpty();
        boolean result2 = str2.isEmpty();
        System.out.println(result1);
        System.out.println(result2);
    }

    /**
     * boolean isBlank()
     * Returns true if the string is empty or contains only white space codepoints, otherwise false.
     * 如果字符串为空或仅包含空格代码点，则返回true，否则返回false
     */
    @Test
    public void isBlank() {
        String str1 = "hello";
        String str2 = " ";
        boolean result1 = str1.isBlank();
        boolean result2 = str2.isBlank();
        System.out.println(result1);
        System.out.println(result2);
    }

    /**
     * int lastIndexOf​(int ch)
     * Returns the index within this string of the last occurrence of the specified character.
     * 返回最后一次出现的指定字符在此字符串内的索引。
     */
    @Test
    public void lastIndexOfTest() {
        String str = "Hello World!";
        int result = str.lastIndexOf('l');
        System.out.println(result);
    }

    /**
     * int length()
     * Returns the length of this string.
     * 返回此字符串的长度。
     */
    @Test
    public void lengthTest() {
        String str = "Hello World!";
        int result = str.length();
        System.out.println(result);
    }

    /**
     * boolean	contains​(CharSequence s)
     * Returns true if and only if this string contains the specified sequence of char values.
     * 当且仅当此字符串包含指定的char值序列时，才返回true。
     */
    @Test
    public void containsTest() {
        String str = "hello world...";
        boolean result = str.contains("wor");
        System.out.println(result);
    }

    /**
     * boolean	startsWith​(String prefix)
     * Tests if this string starts with the specified prefix.
     * 测试此字符串是否以指定的前缀开头
     */
    @Test
    public void startsWithTest() {
        String str = "hello world...";
        boolean result = str.startsWith("hell");
        System.out.println(result);
    }

    /**
     * boolean	equals​(Object anObject)
     * Compares this string to the specified object.
     * 将此字符串与指定对象进行比较。
     */
    @Test
    public void equalsTest() {
        String str = "hello world...";
        boolean result = str.equals("hello world...");
        System.out.println(result);
    }

    /**
     * static String	format​(String format, Object... args)
     * Returns a formatted string using the specified format string and arguments.
     * 使用指定的格式字符串和参数返回格式化的字符串。
     */
    @Test
    public void formatTest() {
        String str = "我叫%s,今年%d岁了";
        String result = String.format(str, "赵四", 58);
        System.out.println(result);
    }

    /**
     * String	toLowerCase()
     * Converts all of the characters in this String to lower case using the rules of the default locale.
     * 将此字符串中的所有字符转换为小写。
     */
    @Test
    public void toLowerCaseTest() {
        String str = "HELLO WORLD!";
        String result = str.toLowerCase();
        System.out.println(result);
    }

    /**
     * String	toUpperCase()
     * Converts all of the characters in this String to upper case using the rules of the default locale.
     * 将此字符串中的所有字符转换为大写。
     */
    @Test
    public void toUpperCaseTest() {
        String str = "hello world!";
        String result = str.toUpperCase();
        System.out.println(result);
    }

    /**
     * static String	valueOf​(boolean b)
     * Returns the string representation of the boolean argument.
     * 返回布尔参数的字符串表示形式
     */
    @Test
    public void valueOfTest() {
        String result = String.valueOf(true);
        System.out.println(result);
    }

    /**
     * String	replace​(char oldChar, char newChar)
     * Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
     * 返回一个字符串，该字符串是用newChar替换此字符串中所有出现的oldChar的结果。
     */
    @Test
    public void replaceTest() {
        String str = "Hello World!";
        String result = str.replace("l", "L");
        System.out.println(result);
    }

    /**
     * String	trim()
     * Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
     * 返回一个值为该字符串的字符串，并删除所有前导和尾随空格。
     */
    @Test
    public void trimTest() {
        String str = "     Hello World!     ";
        String result = str.trim();
        System.out.println(result);
    }

    /**
     * String	substring​(int beginIndex, int endIndex)
     * Returns a string that is a substring of this string.
     * 返回一个字符串，该字符串是该字符串的子字符串。
     */
    @Test
    public void substringTest() {
        String str = "hello world...";
        String result = str.substring(6, 11);
        System.out.println(result);
    }

    /**
     * String	repeat​(int count)
     * Returns a string whose value is the concatenation of this string repeated count times.
     * 返回一个字符串，其值是此字符串的重复计数次数的串联。
     */
    @Test
    public void repeatTest() {
        String str = "hello world!\n";
        String result = str.repeat(10);
        System.out.println(result);
    }
}
